package com.sline.exception;

public class Demo7_Finally {
    /*
    * finally的特点
    *   被finally控制的语句体一定会执行
    *   特殊情况: 在执行到finally之前jvm退出了
    * finally的作用
    *   用于释放资源, 在IO流操作和数据库操作中会见到
     */
    public static void main(String[] args) {

        try {
            System.out.println(1 / 0);
        } catch (Exception e) {
            System.out.println("除数为0");
        } finally {
            System.out.println("看看我执行了吗");
        }

        Demo1 d = new Demo1();
        try {
            d.setAge(-19);
            System.out.println(d.getAge());
        } catch (AgeOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

class Demo1 {
    private String name;
    private int age;

    public Demo1() {
    }

    public Demo1(String name, int age) {

        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeOutOfBoundsException {
        if (age > 0 && age < 150) {
            this.age = age;
        } else {
            throw new AgeOutOfBoundsException("年龄非法");
        }
    }
}

class AgeOutOfBoundsException extends Exception {
    public AgeOutOfBoundsException() {
    }

    public AgeOutOfBoundsException(String message) {
        super(message);
    }
}
